/*
 * DEPRECATED. NO LONGER USED
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package FrontEnd;

import SBLib.IPAddresses;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Ping extends Thread {
    
    final int SLEEP_TIME = 2000;
    final int PING_TIMEOUT = 1000;
    final int BIG_NUMBER = 1000000;
    long currBestTime = BIG_NUMBER; //ridiculous start number
    
    /**
     * Cycles through all the Replication Manager IP addresses and pings them. The server with the best time is used for the next client connection.
     */
    public Ping()
    {
        FEServer.CurrentBestIp = IPAddresses.RMs[0];//default to first one
    }
    @Override
    public void run()
    {
        while(true)
        {
            try 
            {
                for(String ip:IPAddresses.RMs)
                {
                    this.DoPing(ip);
                }
                this.sleep(SLEEP_TIME);
                this.currBestTime = this.BIG_NUMBER;//reset to BIG_NUMBER for the next round of pinging
                
            } 
            catch (InterruptedException ex) 
            {
                Logger.getLogger(Ping.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
    }
    public void DoPing(String ip)
    {
        long start=0, end=0, time;
        try 
        {
            start = System.currentTimeMillis();
            SocketAddress sockaddr = new InetSocketAddress(ip, 9996);
            Socket sock = new Socket();
            sock.connect(sockaddr, this.PING_TIMEOUT);
            
            end = System.currentTimeMillis();
        }
        catch (IOException ex) 
        {
            if(ip.equals(FEServer.CurrentBestIp)) //current best crashed/timed out
            {
                int oldIndex=0, arSize = IPAddresses.RMs.length;
                for(int i=0; i<arSize ; i++)
                {
                    if(IPAddresses.RMs[i].equals(ip))
                    {
                        oldIndex = i;
                        break;
                    }
                }
                int newIndex = oldIndex+1 == arSize ? 0 : oldIndex+1; //set new Best Server to the next one.
                FEServer.CurrentBestIp = IPAddresses.RMs[newIndex];
                this.currBestTime = this.BIG_NUMBER; 
            }
        }
        finally
        {
            time = end - start;
            if(time > 0 && time < this.currBestTime) //if there was no fail, and new best time.
            {
                FEServer.CurrentBestIp = ip;
                this.currBestTime = time;
                System.out.println("New Best Ip is: "+ip+" with time of "+time+" (ms)");
            }
        }
       
                
    }
}
